package yxy.game.pm2.active.handle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import org.redisson.api.RMap;
import yxy.cherry.battle.module.Position;
import yxy.cherry.battle.module.record.FighterPosition;
import yxy.cherry.battle.module.record.FighterSide;
import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actchallenge;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.ChanllengeVideoModule;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.BattleMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

//import yxy.cherry.data.DataCenter;

/**
 * 限时挑战
 * @author Dell
 *
 */
public class Act_TimeChallengeHandle extends BattleModuleHandle {

	protected String code() {
		return BattleType.ACT_TIMECHALLENGE.name();
	}

	public BattleMessage.BattleResponse battleResponse(String playerCode, int id) throws Exception {
		BattleHandle battleHandle = new BattleHandle();
		BattleModule battleModule = createBattleModule(playerCode, id);
		return battleHandle.battleResponse(battleModule);
	}
	
	public BattleMessage.BattleExecuteResponse battleResponseExecute(String playerCode, BattleMessage.BattleExecuteRequest request) throws Exception {
		BattleModule battleModule = BattleModule.get(playerCode, code());

		try {
			int[] angersTeamA = null;
			if (request != null) {
				List<BattleMessage.FighterInfo> fighterInfos = request.getFormatList();
				ArrayList<BattleMessage.FighterInfo> fighterInfosTeamA = new ArrayList<>();
				for (BattleMessage.FighterInfo fighterInfo : fighterInfos) {
					FighterPosition fp = FighterPosition.forNumber(fighterInfo.getPosition().getNumber());
					if (fp.getSide().equals(FighterSide.LEFT)) {
						fighterInfosTeamA.add(fighterInfo);
					}
				}
				angersTeamA = new BattleHandle().angersFromFormatList(fighterInfosTeamA);
			}

		} catch (Exception e) {
			
		} finally {
			
		}
		
		BattleMessage.BattleExecuteResponse response = new BattleHandle().battleResponseExecute(battleModule, null, null);
		return response;
	}

	public ActiveMessage.ActChanllengeExecuteResponse ActBattleResponseExecute(ActiveMessage.ActChanllengeExecuteResponse.Builder response, String playerCode, BattleMessage.BattleExecuteRequest request) throws Exception {
		BattleModule battleModule = BattleModule.get(playerCode, code());

		try {
			int[] angersTeamA = null;
			if (request != null) {
				List<BattleMessage.FighterInfo> fighterInfos = request.getFormatList();
				ArrayList<BattleMessage.FighterInfo> fighterInfosTeamA = new ArrayList<>();
				for (BattleMessage.FighterInfo fighterInfo : fighterInfos) {
					FighterPosition fp = FighterPosition.forNumber(fighterInfo.getPosition().getNumber());
					if (fp.getSide().equals(FighterSide.LEFT)) {
						fighterInfosTeamA.add(fighterInfo);
					}
				}
				angersTeamA = new BattleHandle().angersFromFormatList(fighterInfosTeamA);
			}

			BattleMessage.BattleExecuteResponse battleresp = new BattleHandle().battleResponseExecute(battleModule,	angersTeamA, null);
			SettlementTimeChallenge settlement = (SettlementTimeChallenge) battleModule.getSettlement();
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(settlement.getRewards());

			response.setBattleResp(battleresp);
			response.setId(settlement.getId());
			response.addAllRewards(items);
			response.setResult(Result.Success.packResult());

			if(battleModule.getBattleResult().getNumber() == BattleResult.LEFT_WIN.getNumber()) {
				AreaBean ab = AreaModule.area(playerCode);
				ChanllengeVideoModule.get(ab.getAreaId(), settlement.getId()).record(playerCode, battleModule, settlement.getId());				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		
		return response.build();
	}
	
	/**
	 * 创建战斗模块
	 * @param playerCode
	 * @param id
	 * @return
	 */
	public BattleModule createBattleModule(String playerCode, int id) {
		BattleHandle battleHandle = new BattleHandle();

		BattleModule battleModule = BattleModule.create(playerCode);
		battleModule.setType(BattleType.ACT_TIMECHALLENGE);
		battleModule.setCode(code());

		// 玩家的team
		TeamModule teamA = battleHandle.teamPlayer(playerCode, FormatType.普通);
		battleModule.setTeamA(teamA);

		// 征战地图
		TeamModule teamB = teamTimeChallenge(id, playerCode);
		battleModule.setTeamB(teamB);

		battleModule.setSettlement(SettlementTimeChallenge.create(playerCode, id));
		battleModule.set(1);

		return battleModule;
	}

	public TeamModule teamTimeChallenge(int id, String playercode) {
		actchallenge execl = ActDataCenter.getData(id, actchallenge.class, playercode);
		TeamModule team = new TeamModule();

		ArrayList<FighterModule> fighterModuleList = getFighterModuleList(id);
		// 武将
		for (int i = 0; i < execl.getHeroid().length; i++) {
			Position position = Position.forValue(i + 1);
			int heroid = execl.getHeroid()[i];
			if (heroid == -1) continue;
			FighterModule fighterModule = fighterModuleList.get(i);
			team.addfighter(position, fighterModule);
		}

		// 神器
		WeaponModule weapon = WeaponModule.create(execl.getWeapondataid(), execl.getWeaponlv(), 0, 0, execl.getWeaponskilllv());
		team.setWeapon(weapon);
		
		//信息
		herodata heroexecl = DataCenter.getData(execl.getMain(), herodata.class);
		team.setName(heroexecl.getName());
		team.setHead(execl.getHeadid());
		team.setPower(team.power());
		
		return team;
	}

	public static Collection<Currency> getRewards(int id, String playercode) {
		ArrayList<Currency> rewards = new ArrayList<>();
		actchallenge execl = ActDataCenter.getData(id, actchallenge.class, playercode);
		if (execl == null) {
			return rewards;
		}
		
		rewards.addAll(Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum()));
		return rewards;
	}



	//获取内存怪物战力属性
	public ArrayList<FighterModule> getFighterModuleList(int id){
		RMap<String, ArrayList<FighterModule>> rMap = RedisManager.getInstance().getConnection(RedisType.REDIS_DATA).client().getMap("INIT:actchallenge");
		JSONArray arrary = (JSONArray) JSONArray.toJSON(rMap.get(String.valueOf(id)));
		ArrayList<FighterModule> fighterModules = JSON.parseObject(arrary.toString(), new TypeReference<ArrayList<FighterModule>>() {
		});
		return fighterModules;
	}
}
